<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>诚聘英才</cite></a>
    </div>
</div>
<style>

</style>

<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-userfront-formlist">
                <div class="demoTable">
                    职位名称：
                    <div class="layui-inline">
                        <input class="layui-input" name="positionName" id="positionName" autocomplete="off">
                    </div>
                    工作地点：
                    <div class="layui-inline">
                        <input class="layui-input" name="workPlace" id="workPlace" autocomplete="off">
                    </div>
                    招聘类型：
                    <div class="layui-inline">
                        <select name="recruitType" value="{{ d.params.recruitType || '' }}" lay-verify="required" lay-search="">
                            <option value="">直接选择或搜索选择</option>
                            <option value="1">校园招聘</option>
                            <option value="2">社会招聘</option>
                          </select>
                    </div>
                    <button class="layui-btn" data-type="reload" lay-submit lay-filter="LAY-user-front-search">搜索</button>
                    <script type="text/html" id="toolbarDemo">
                        <div class="layui-btn-container">

                            <button type="button" class="layui-btn" id="addBtn" lay-event="addBtn">添加信息</button>
                            <button type="button" class="layui-btn" id="deleBtn" lay-event="getCheckLength">批量删除</button>
                        </div>
                    </script>

                </div>
            </div>
            <div class="layui-fluid">
                <div class="layui-card">
                    <div class="layui-card-body">
                        <table class="layui-hide" id="table-instance" lay-filter="table-instance"></table>
                        <script type="text/html" id="imgTpl">
                            <img style="display: inline-block; width: 50%; height: 100%;" src={ { d.avatar }}>
                        </script>
                        <script type="text/html" id="table-useradmin-webuser">
                            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                        </script>
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

<script>
    layui.use(['layedit', 'admin', 'upload', 'laydate', 'useradmin', 'table', 'form'], function() {
        var $ = layui.$,
            admin = layui.admin,
            setter = layui.setter,
            view = layui.view,
            table = layui.table,
            form = layui.form,
            leyer = layui.leyer,
            upload = layui.upload,
            laydate = layui.laydate,
            layedit = layui.layedit;

        form.render(null, 'layadmin-userfront-formlist');
        //监听搜索
        form.on('submit(LAY-user-front-search)', function(data) {
            var field = data.field;
            //执行重载
            getData('/recruit/admin/search', field)
                // table.reload('table-instance', {
                //     where: field
                // });
        });


        //监听工具栏操作（新增，批量删除等）
        table.on('toolbar(table-instance)', function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'addBtn':
                    admin.popup({
                        title: '添加信息',
                        area: ['800px', '900px'],
                        id: 'LAY-popup-useradmin-add',
                        url: setter.request.baseUrl + "/recruit/admin/add",
                        success: function(layero, index) {
                            view(this.id).render('user/user/table').done(function() {
                                form.render(null, 'layuiadmin-form-admin');

                                laydate.render({
                                    elem: '#date'
                                });
                                laydate.render({
                                    elem: '#date1'
                                });

                                layedit.set({
                                    uploadImage: {
                                        url: setter.request.baseUrl + '/upload/admin/file', //接口url
                                        type: 'post' //默认post
                                    }
                                });

                                var aa = layedit.build('demo');

                                //监听提交
                                form.on('submit(LAY-user-front-submit)', function(data) {

                                    //时间戳  时间转时间戳
                                    var field = data.field; //获取提交的字段

                                    var aahtml = layedit.getContent(aa);
                                    field.detail = aahtml

                                    var releaseTime = field.releaseTime;
                                    var EndTime = field.endTime;
                                    releaseTime = releaseTime.substring(0, 10)
                                    EndTime = EndTime.substring(0, 10)
                                    field.releaseTime = new Date(releaseTime).getTime() / 1000;
                                    field.endTime = new Date(EndTime).getTime() / 1000;


                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                    //$.ajax({});
                                    adminAdd(field);
                                    layui.table.reload('LAY-user-back-manage'); //重载表格
                                    layer.close(index); //执行关闭 
                                });
                            });
                        }
                    });
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data,
                        arr = [];
                    if (data.length == 0) {
                        layer.msg('未选中数据');
                        return;
                    }
                    for (let i = 0; i < data.length; i++) {
                        arr.push(data[i].recruitId)
                    }
                    var params = {
                        ids: arr
                    }
                    admin.req({
                        url: setter.request.baseUrl + "/recruit/admin/deleteall",
                        data: JSON.stringify(params),
                        type: "post",
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        done: function(res) {
                            layer.msg("删除成功"); //删除成功之后弹窗
                            table.reload('table-instance', {});
                        },
                    });
                    break;
            };
        });




        $("#search").on('click', function() {

            admin.popup({
                title: '添加信息',
                area: ['800px', '900px'],
                id: 'LAY-popup-useradmin-add',
                url: setter.request.baseUrl + "/recruit/admin/add",
                success: function(layero, index) {
                    view(this.id).render('user/user/table').done(function() {
                        form.render(null, 'layuiadmin-form-admin');


                        //监听提交
                        form.on('submit(LAY-user-front-submit)', function(data) {


                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            //$.ajax({});
                            adminAdd(field);
                            layui.table.reload('LAY-user-back-manage'); //重载表格
                            layer.close(index); //执行关闭 
                        });
                    });
                }

            })
        });

        getData()

        function getData(searchurl, where) {

            if (!searchurl) {
                var url = setter.request.baseUrl + "/recruit/admin/list";
                var where = {
                    typeId: 1
                }
            } else {
                var url = setter.request.baseUrl + searchurl;
                var where = where;

            }
            table.render({
                elem: "#table-instance",
                url: url,
                method: "get",
                headers: {
                    token: layui.data("layuiAdmin").token
                },
                toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
                response: {
                    statusName: "code",
                    statusCode: 0,
                    msgName: "msg",
                    countName: "count",
                    dataName: "list",
                },
                where: where,
                cols: [
                    [{
                        type: 'checkbox',
                        fixed: 'left'
                    }, {
                        align: 'center',
                        field: "positionNo",
                        width: "7%",
                        title: "岗位编号",
                        sort: true
                    }, {
                        align: 'center',
                        field: "positionName",
                        width: "8%",
                        title: "职位名称"
                    }, {
                        align: 'center',
                        field: "releaseTime",
                        width: "8%",
                        title: "发布时间",
                        templet: function(d) {
                            var star_releaseTime = layui.util.toDateString(new Date(d.releaseTime * 1000).getTime(), "yyyy/MM/dd");
                            return '<div>' + star_releaseTime + '</div>'
                        }
                    }, {
                        align: 'center',
                        field: "endTime",
                        width: "8%",
                        title: "截止时间",
                        templet: function(d) {
                            var star_endTime = layui.util.toDateString(new Date(d.endTime * 1000).getTime(), "yyyy/MM/dd");
                            return '<div>' + star_endTime + '</div>'
                        }
                    }, {
                        align: 'center',
                        field: "sex",
                        width: "8%",
                        title: "性别",
                        templet: function(res) {
                            var a = '';
                            if (res.sex == 0) {
                                a = '男'
                            } else if (res.sex == 1) {
                                a = '女'
                            }
                            return a
                        }
                    }, {
                        align: 'center',
                        field: "education",
                        width: "8%",
                        title: "学历要求"
                    }, {
                        align: 'center',
                        field: "workExperience",
                        width: "8%",
                        title: "工作经验"
                    }, {
                        align: 'center',
                        field: "monthSalary",
                        width: "8%",
                        title: "月薪"
                    }, {
                        align: 'center',
                        field: "workPlace",
                        title: "工作地点",
                        width: "8%"
                    }, {
                        align: 'center',
                        field: "recruitType",
                        width: "8%",
                        title: "招聘类型",
                        templet: function(res) {
                            var a = '';
                            if (res.recruitType == 1) {
                                a = '校园招聘'
                            } else if (res.recruitType == 2) {
                                a = '社会招聘'
                            }
                            return a
                        }
                    }, {
                        field: "detail",
                        width: "8%",
                        title: "详情"
                    }, {
                        align: 'center',
                        toolbar: "#table-useradmin-webuser" //删除
                    }]
                ],
                page: true, //分页
            });
        }

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        //删除模块
        table.on("tool(table-instance)", function(obj) {
            var data = obj.data;

            if (obj.event === "del") {
                layer.confirm("真的删除么", function(index) {
                    delAdminHandle({
                        recruitId: data.recruitId,
                    });

                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === "edit") {
                admin.popup({
                    title: "编辑用户",
                    area: ["800px", "800px"],
                    success: function(layero, index) {
                        view(this.id)
                            .render("user/user/orderform", data)
                            .done(function() {
                                let _data = data;
                                console.log(_data)
                                form.render(null, "layuiadmin-form-admin");
                                //日历
                                laydate.render({
                                    elem: '#date'
                                });
                                laydate.render({
                                    elem: '#date1'
                                });
                                var releaseTime = layui.util.toDateString(new Date(_data.releaseTime * 1000).getTime(), "yyyy-MM-dd");
                                laydate.render({
                                    elem: '#date',
                                    value: releaseTime,
                                    isInitValue: true
                                });

                                var endTime = layui.util.toDateString(new Date(_data.endTime * 1000).getTime(), "yyyy-MM-dd");
                                laydate.render({
                                    elem: '#date1',
                                    value: endTime,
                                    isInitValue: true
                                });
                                // 给编辑页面赋值
                                if (data.workExperience) {
                                    $("select[name='workExperience'] option[value=" + data.workExperience + "]").attr("selected", true)
                                }
                                if (data.education) {
                                    $("select[name='education'] option[value='" + data.education + "']").attr("selected", true)
                                }
                                if (data.monthSalary) {
                                    $("select[name='monthSalary'] option[value=" + data.monthSalary + "]").attr("selected", true)
                                }
                                if (data.recruitType) {
                                    $("select[name='recruitType'] option[value=" + data.recruitType + "]").attr("selected", true)
                                }
                                layui.form.render('select')
                                    // $("#").text("html")
                                layedit.set({
                                    uploadImage: {
                                        url: setter.request.baseUrl + '/upload/admin/file', //接口url
                                        type: 'post' //默认post
                                    }
                                });
                                //富文本
                                var aa = layedit.build('demo');
                                layedit.setContent(aa, data.detail, false)
                                    //监听提交
                                form.on("submit(LAY-user-front-submit)", function(data) {
                                    var field = data.field;
                                    var aahtml = layedit.getContent(aa);
                                    field.detail = aahtml
                                    var releaseTime = field.releaseTime;
                                    var EndTime = field.endTime;
                                    releaseTime = releaseTime.substring(0, 10)
                                    EndTime = EndTime.substring(0, 10)
                                    field.releaseTime = new Date(releaseTime).getTime() / 1000;
                                    field.endTime = new Date(EndTime).getTime() / 1000;
                                    editUserHandle(Object.assign(_data, field));
                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                    // adminAdd(field);
                                    layui.table.reload("table-instance");
                                    layer.close(index);
                                });
                            });
                    },
                });
            }
        });

        //删除
        function delAdminHandle(params) {
            console.log(params)

            //  debugger
            admin.req({
                //*****************需要替换****************
                url: setter.request.baseUrl + "/recruit/admin/delete",
                data: {
                    ...params,
                },
                type: "get",
                done: function(res) {
                    layer.msg("删除成功"); //删除提示框
                },
            });
        }

        //修改编辑
        function editUserHandle(params) {
            admin.req({
                url: setter.request.baseUrl + "/recruit/admin/update",
                data: JSON.stringify(params),
                type: "post",
                dataType: "json",
                contentType: "application/json",
                done: function(res) {
                    layui.table.reload("table-instance");
                },
            });
        }

        //添加
        function adminAdd(params) {
            admin.req({
                url: setter.request.baseUrl + "/recruit/admin/add",
                data: JSON.stringify(params),
                type: "post",
                dataType: "json",
                contentType: "application/json",
                done: function(res) {
                    layui.table.reload("table-instance");
                },
            });
        }

    })
</script>